module CheckoutHelper


  # Laat alleen de eerste 5 nummers van een creditcard zien.
  def format_checkout_creditcard_number(number)

    if number.size > 5
      number[0,5] + '-' + 'x' * (number.size-5)
    else
      number
    end

  end

  # De verloopdatum van een creditcard heeft alleen een maand/jaar
  def select_checkout_creditcard_expiration_date(creditcard)

    s = ''

    if creditcard.expiration_date
      year  = creditcard.expiration_date.year
      month = creditcard.expiration_date.month
    else
      year  = Time.now.year
      month = 1
    end

    s += hidden_field_tag( 'checkout_payment_creditcard[expiration_date(3i)]', 1 )
    s += select_tag( 'checkout_payment_creditcard[expiration_date(2i)]', options_for_select(1..12, month) )
    s += select_tag( 'checkout_payment_creditcard[expiration_date(1i)]', options_for_select((Time.now.year-10)..(Time.now.year+10), year) )


    s
  end

  # Opties voor adres
  def options_for_checkout_address_select(addresses)

    options = [['- Nieuw adres -', 0]]

    addresses.each do |address|
      options <<= [h(address.name + ' (' + address.human_readable + ')'), address.id]
    end

    options_for_select(options)
  end

  # Opties voor betalen.
  def options_for_checkout_payment_select
    options_for_select([
      ['- Kies -', 0],
      ['Creditcard', 1],
      ['Op rekening zetten', 2]
    ])
  end


  # De checkout bestaat uit meerdere stappen, laat zien op welke stap de gebruiker is.
  def checkout_progress(step)

    p = case step
      when :begin then 0
      when :contact then 1
      when :address then 2
      when :payment then 3
      when :end then 4
      else 0
    end

    return '<div class="checkout-progress"><ul>
      <li class="' + checkout_progress_class(p, 0) + '">Winkelwagentje</li>
      <li class="' + checkout_progress_class(p, 1) + '">Gegegevens</li>
      <li class="' + checkout_progress_class(p, 2) + '">Adres</li>
      <li class="' + checkout_progress_class(p, 3) + '">Betaalmethode</li>
      <li class="' + checkout_progress_class(p, 4) + '"">Bevestigen</li>
    </ul></div>'

  end

  private
    def checkout_progress_class(x, n)
      if x < n
        'future'
      elsif x == n
        'current'
      else #x > n
        'past'
      end
    end

end
